from collections import OrderedDict
from xml.etree import ElementTree

# user config block
List_uint8  = ['uint8', 'UINT8', 'UInt8', 'Uint8','uint8_T']
List_uint16 = ['uint16','UINT16','UInt16','Uint16','uint16_T']
List_uint32 = ['uint32','UINT32','UInt32','Uint32','uint32_T']
List_float32  = ['Real32','FLOAT','float32','float','real32_T','real_T']
List_bool   = ['BOOL','bool','BOOLEAN','boolean','boolean_T']
List_uint64 = ['uint64','UINT64','UInt64','Uint64','uint64_T']

List_Base = ['uint8','uint16','uint32','float32','uint64','boolean']

#定义类的属性时，使用首字母大写的方式，防止后续在调用的过程中写错

# 定义一个类，叫做head file
class headfile(object):
    def __init__(self,File_Name):
        self.File_Name = File_Name

        #定义一个字典，用来存放 文件中解析到的结构体
        self.Struct = OrderedDict()

        #定义一个字典，用来存放 文件中解析到的宏定义
        self.Define = OrderedDict()

        # Flag 当循环文件遍历找到'typedef struct'时置true
        # 找到'}*;'时置 Flase
        self.Find_Strut_Flag = False
        self.Last_Strut_Flag = False

    def Gen_XML_Frame(self,root,RTE_FileName):
    # 添加 xml文件的开头
    # 这里一行一行写应该就可以了
#    <AR-PACKAGES>
#        <AR-PACKAGE>
#            <SHORT-NAME>vehinfo</SHORT-NAME>
#            <AR-PACKAGES>
#                <AR-PACKAGE>
#                    <SHORT-NAME>DataTypes</SHORT-NAME>
#                    <ELEMENTS>    
        Packages  = ElementTree.SubElement(root,"AR-PACKAGES")
        Package   = ElementTree.SubElement(Packages,"AR-PACKAGE")
        File_ShortName = ElementTree.SubElement(Package,"SHORT-NAME")
        File_ShortName.text = RTE_FileName
        DataType_Packages  = ElementTree.SubElement(Package,"AR-PACKAGES")
        DataType_Package   = ElementTree.SubElement(DataType_Packages,"AR-PACKAGE")
        DataType_ShortName = ElementTree.SubElement(DataType_Package,"SHORT-NAME")
        DataType_ShortName.text = 'DataTypes'

        Element     = ElementTree.SubElement(DataType_Package,"ELEMENTS")

        return Element

class struct(object):
    def __init__(self):
        self.Type = ''
        self.Name = ''

        #定义字典，用来存放 结构体的 成员/数组成员/其他子结构体
        self.Member        = OrderedDict()
        self.Member_Array  = OrderedDict()
        #结构体成员 和 变量成员 可能生成的代码类似 暂时不处理
        #self.Member_Struct = OrderedDict()

    def Gen_Struct_Start(self,Element,st_index,Category_text):
        Struct_Start = ElementTree.SubElement(Element,"IMPLEMENTATION-DATA-TYPE")
        Struct_DataType_ShortName = ElementTree.SubElement(Struct_Start,"SHORT-NAME")
        Struct_DataType_ShortName.text = st_index
        Category = ElementTree.SubElement(Struct_Start,"CATEGORY")
        Category.text = Category_text
        Sub_Elements = ElementTree.SubElement(Struct_Start,"SUB-ELEMENTS")

        return Sub_Elements

class array(object):
    def __init__(self,Type,Name,Size):
        self.Type = Type
        self.Name = Name
        self.Size = Size
    
    def TpyeJuge(self):
        if self.Type in List_uint8:
            return 'uint8'
        elif self.Type in List_uint16:
            return 'uint16'
        elif self.Type in List_uint32:
            return 'uint32'
        elif self.Type in List_float32:
            return 'float32'
        elif self.Type in List_bool:
            return 'boolean'
        elif self.Type in List_uint64:
            return 'uint64'
        else :
            # 结构体类型
            return self.Type
        
    def BaseTypeJudge(slef,type_temp):
        if type_temp in List_Base:
            return True
        else:
            return False

class member(object):
    def __init__(self,Type,Name):
        self.Name = Name
        self.Type  = Type

    def TpyeJuge(self):
        if self.Type in List_uint8:
            return 'uint8'
        elif self.Type in List_uint16:
            return 'uint16'
        elif self.Type in List_uint32:
            return 'uint32'
        elif self.Type in List_float32:
            return 'float32'
        elif self.Type in List_bool:
            return 'boolean'
        elif self.Type in List_uint64:
            return 'uint64'
        else :
            # 结构体类型
            return self.Type
        
    def BaseTypeJudge(slef,type_temp):
        if type_temp in List_Base:
            return True
        else:
            return False

class define(object):
    def __init__(self,Macro,Value):
        self.Macro = Macro
        self.Value = Value